package com.baaio.conn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
 * 解析查询语句类
 *
 */
public class GetSqlString
{

	public String dbSelect(String tableName, String fields, String vlaue)
	{

		String sql = "";
		if (fields.equals("") || fields == null)
		{
			sql = "select * from " + tableName;
		}
		else
		{
			sql = "select " + "*" + " from " + tableName + " where " + fields
					+ "='" + vlaue + "'";
		}

		return sql;
	}

	public String dbSelect(String tableName, String item, String fields,
			String vlaue)
	{

		String sql = "";
		if (fields.equals("") || fields == null || item.equals("")
				|| item == null)
		{
			sql = "select * from " + tableName;
		}
		else
		{
			sql = "select " + item + " from " + tableName + " where " + fields
					+ "='" + vlaue + "'";
		}

		return sql;
	}

	public String dbSelect(String tableName, ArrayList<String> fields,
			String selCondition)
	{

		String selFields = "";
		String selFieldsTem = "";
		if (fields == null || fields.isEmpty())
		{
			selFieldsTem = "*";
		}
		else
		{
			for (int i = 0; i < fields.size(); ++i)
			{
				if (fields.get(i) == "" || fields.get(i) == null)
				{
					selFieldsTem = "*";
				}
				else
				{
					selFields += fields.get(i) + ", ";
					selFieldsTem = selFields.substring(0,
							selFields.length() - 2);// 根据String的索引提取子串
				}
			}
		}
		String sql = "select " + selFieldsTem + " from " + tableName + " "
				+ selCondition;

		return sql;
	}

	public String queryData(String sql)
	{

		return sql;
	}

	public String delete(String sql)
	{

		return sql;
	}

	public String dbDelete(String tableName, String condition)
	{

		String sql = "delete from " + tableName + " " + condition;

		return sql;
	}

	public String dbDelete(String tableName, String dcName, String value)
	{

		String sql = "delete from " + tableName + " " + "where " + dcName
				+ "='" + value + "'";
		return sql;
	}
	
	public String dbDelete(String tableName)
	{

		String sql = "truncate table " + tableName;

		return sql;
	}

	public ArrayList<String> dbDelete(String tableName, String cName,
			ArrayList<String> list)
	{

		ArrayList<String> list2 = new ArrayList<String>();
		for (String sql2 : list)
		{
			list2.add(dbDelete(tableName, cName, sql2));

		}
		return list2;

	}

	public String dbUpdate(String tabName, HashMap<String, String> reCount,
			String upCondition)
	{

		String Values = "";
		Iterator<Entry<String, String>> keyValues = reCount.entrySet()
				.iterator();
		for (int i = 0; i < reCount.size(); ++i)
		{
			Map.Entry<String, String> entry = (Map.Entry<String, String>) keyValues
					.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			Values += key + "=" + "'" + value + "'" + ", ";
		}
		String updateValues = Values.substring(0, Values.length() - 2);
		String sql = "update " + tabName + " set " + updateValues + " "
				+ upCondition;

		return sql;

	}

	public String dbInsert(String tabName, HashMap<String, String> values)
	{

		String sql = "";
		String insertFields = "", temFields = "";
		String insertValues = "", temValues = "";
		Iterator<Entry<String, String>> keyValues = values.entrySet()
				.iterator();
		for (int i = 0; i < values.size(); ++i)
		{
			Map.Entry<String, String> entry = (Map.Entry<String, String>) keyValues
					.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			temFields += key + ", ";
			temValues += "'" + value + "'" + ", ";
		}
		insertFields = temFields.substring(0,
				((Integer) (temFields.length()) - 2));
		insertValues = temValues.substring(0, temValues.length() - 2);
		sql = "insert into " + tabName + " (" + insertFields + ") values"
				+ " (" + insertValues + ");";

		return sql;

	}

}
